import {BrowserWindow, Menu, app, dialog} from 'electron'
import {platform} from "os"
import menuconfig from '../config/menu'
import {openDevTools, IsUseSysTitle, UseStartupChart} from '../config/const'
import setIpc from './ipcMain'
import {winURL, loadingURL} from '../config/StaticPath'
import path from "path";
import {setParams, getParams} from './store'


var loadWindow = null
var mainWindow = null
setIpc.Mainfunc(IsUseSysTitle)


const args = [];//介绍说是win需要的参数

if (!app.isPackaged) {
    args.push(path.resolve(process.argv[1]));
}

args.push('--');

function createMainWindow(par) {
    /**
     * Initial window options
     */
    console.log(par)

    par.forEach(item => {
        setParams(item.key, item.value)
    })
    // setParams('params', par)

    mainWindow = new BrowserWindow({
        height: 800,
        useContentSize: true,
        width: 1000,
        resizable : false,
        maximizable     : false,
        show: false,
        // show: true,
        frame: false,
        titleBarStyle: platform().includes('win32') ? 'default' : 'hidden',
        webPreferences: {
            contextIsolation: false,
            nodeIntegration: true,
            webSecurity: false,
            // 如果是开发模式可以使用devTools
            devTools: process.env.NODE_ENV === 'development' || openDevTools,
            // devTools: true,
            // 在macos中启用橡皮动画
            scrollBounce: process.platform === 'darwin'
        }
    })
    // 这里设置只有开发环境才注入显示开发者模式
    if (process.env.NODE_ENV === 'development' || openDevTools) {
        menuconfig.push({
            label: '开发者设置',
            submenu: [{
                label: '切换到开发者模式',
                accelerator: 'CmdOrCtrl+I',
                role: 'toggledevtools'
            }]
        })
    }
    // 载入菜单
    const menu = Menu.buildFromTemplate(menuconfig)
    Menu.setApplicationMenu(menu)
    mainWindow.loadURL(winURL)

    mainWindow.webContents.once('dom-ready', () => {
        mainWindow.show()
        if (process.env.NODE_ENV === 'development' || openDevTools) mainWindow.webContents.openDevTools(true)
        if (UseStartupChart) loadWindow.destroy()
    })

    mainWindow.on('maximize', () => {
        mainWindow.webContents.send("w-max", true)
    })
    mainWindow.on('unmaximize', () => {
        mainWindow.webContents.send("w-max", false)
    })
    mainWindow.on('closed', () => {
        mainWindow = null
        app.quit();
    })
    mainWindow.show();

    // const token = getParams()
    // setTimeout(() => {
    //     // openfile 允许选择文件
    //     // openDirectory 允许选择文件夹
    //     // multiSelection  允许多选
    //     // showHiddenFiles 显示隐藏文件
    //     // createDirectory 允许创建文件夹
    //
    //     dialog.showOpenDialog({
    //         title: token,
    //         properties: ["openfile", 'multiSelections']
    //     }).then((results) => {
    //         console.log(results.filePaths);
    //         console.log(results.canceled);
    //     })
    //
    // }, 2000);


}

function loadingWindow(par) {
    loadWindow = new BrowserWindow({
        width: 400,
        height: 600,
        frame: false,
        backgroundColor: '#222',
        skipTaskbar: true,
        transparent: true,
        resizable: false,
        webPreferences: {experimentalFeatures: true}
    })

    loadWindow.loadURL(loadingURL)

    loadWindow.show()

    setTimeout(() => {
        createMainWindow(par)
    }, 1000)

    loadWindow.on('closed', () => {
        loadWindow = null
    })
}

function initWindow(par) {
    if (UseStartupChart) {
        return loadingWindow(par)
    } else {
        return createMainWindow(par)
    }
}

export default initWindow
